package 牛客.二叉树;

import 剑指offer.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class BM23二叉树的前序遍历 {
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        List<Integer> list=new ArrayList<>();

        preOrder(root,list);

        int[] arr=new int[list.size()];
        for (int i = 0; i <list.size(); i++) {
            arr[i]=list.get(i);
        }
        return arr;
    }

    private void preOrder(TreeNode root, List<Integer> list) {
        if (root==null) {
            return;
        }
        list.add(root.val);
        preOrder(root.left,list);
        preOrder(root.right,list);
    }
}
